package com.nibianplus.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;

import com.nibianplus.volley.VolleyHttpClient;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

/**
 * Activity
 * 
 */
public abstract class BaseActivity extends Activity implements BaseObjectListener {

	BaseObject dataObject;

	public boolean isCan = true;

	@Override
	final protected void onCreate(Bundle savedInstanceState) {

		preOnCreate(savedInstanceState);
		super.onCreate(savedInstanceState);

		onCreateHE(savedInstanceState);
	}

	protected void preOnCreate(Bundle savedInstanceState) {
	}

	public ViewGroup getRootView() {
		return (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
	}

	protected abstract void initView();

	protected abstract BaseObject createDataObject();

	protected void onCreateHE(Bundle savedInstanceState) {
		initView();

		dataObject = createDataObject();
		dataObject.setListener(this);
		dataObject.refresh();
	}

	public BaseObject getDataObject() {
		return dataObject;
	}

	@Override
	final protected void onStart() {

		super.onStart();
		onStartHE();
	}

	protected void onStartHE() {

	}

	@Override
	final protected void onRestart() {
		super.onRestart();
		onRestartHE();
	}

	protected void onRestartHE() {

	}

	@Override
	final protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		onResumeHE();
	}

	protected void onResumeHE() {
	}

	@Override
	final protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		onPasueHE();
	}

	protected void onPasueHE() {

	}

	@Override
	final protected void onStop() {
		super.onStop();
		onStopHE();
	}

	protected void onStopHE() {

	}

	public String getTagName() {
		return getClass().getName();
	}

	@Override
	final protected void onDestroy() {
		super.onDestroy();
		VolleyHttpClient.cancalAll(getTagName());
		onDestroyHE();
	}

	protected void onDestroyHE() {

	}

	@Override
	final protected void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		onSaveInstanceStateHE(savedInstanceState);
	}

	protected void onSaveInstanceStateHE(Bundle savedInstanceState) {

	}

	abstract public void refreshUI(BaseObject dataObject, int tag);

	public void dataObjectChanged(BaseObject dataObject, int what) {
		refreshUI(dataObject, what);
	}

	public void dataObjectFailed(BaseObject dataObject, int what, JSONObject response) {

	}



}
